package assemblers.beans;

import assemblers.interfaces.AddressInfoAssembler;
import infos.AddressInfo;
import model.Address;
import model.Person;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

@Stateless(name = "AddressInfoAssembler")
public class AddressInfoAssemblerBean implements AddressInfoAssembler {

    @Override
    public AddressInfo assemble(Address address) {
        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setApartment(address.getApartment());
        addressInfo.setBuilding(address.getBuilding());
        addressInfo.setCity(address.getCity());
        addressInfo.setId(address.getId());
        addressInfo.setStreet(address.getStreet());
        addressInfo.setPersonsIds(getPersonsIds(address));
        return addressInfo;
    }

    @Override
    public List<AddressInfo> assembleList(List<Address> addresses) {
        List<AddressInfo> addressInfos = new ArrayList<AddressInfo>();
        for (Address address : addresses){
            AddressInfo addressInfo = assemble(address);
            addressInfos.add(addressInfo);
        }
        return addressInfos;
    }

    private List<Long> getPersonsIds(Address address) {
        List<Long> idList = new ArrayList<Long>();
        for (Person person : address.getPersons()) {
            Long id = person.getId();
            idList.add(id);
        }
        return idList;
    }
}
